import Mock from 'mockjs'

// 商品分类数据
const categories = [
  {
    id: '1',
    name: '宠物用品',
    parentId: '0'
  },
  {
    id: '2',
    name: '宠物食品',
    parentId: '0'
  },
  {
    id: '3',
    name: '宠物玩具',
    parentId: '0'
  },
  {
    id: '11',
    name: '猫咪用品',
    parentId: '1'
  },
  {
    id: '12',
    name: '狗狗用品',
    parentId: '1'
  },
  {
    id: '21',
    name: '猫粮',
    parentId: '2'
  },
  {
    id: '22',
    name: '狗粮',
    parentId: '2'
  },
  {
    id: '23',
    name: '零食',
    parentId: '2'
  },
  {
    id: '31',
    name: '互动玩具',
    parentId: '3'
  },
  {
    id: '32',
    name: '益智玩具',
    parentId: '3'
  }
]

// 生成商品列表
const generateProductList = (params) => {
  const { page = 1, limit = 10, keyword = '', status = '', isOnShelf = '' } = params
  
  const count = 35
  const list = []
  
  for (let i = 0; i < count; i++) {
    const productStatus = Mock.Random.pick(['1', '2', '3', '4'])
    const productShelf = Mock.Random.boolean()
    
    // 根据筛选条件过滤
    if ((status && productStatus !== status) || 
        (isOnShelf !== '' && String(productShelf ? '1' : '0') !== isOnShelf)) {
      continue
    }
    
    const categoryId = Mock.Random.pick(['11', '12', '21', '22', '23', '31', '32'])
    let categoryPath = ''
    
    // 构建分类路径
    if (categoryId.startsWith('1')) {
      categoryPath = '宠物用品 > ' + (categoryId === '11' ? '猫咪用品' : '狗狗用品')
    } else if (categoryId.startsWith('2')) {
      if (categoryId === '21') categoryPath = '宠物食品 > 猫粮'
      else if (categoryId === '22') categoryPath = '宠物食品 > 狗粮'
      else categoryPath = '宠物食品 > 零食'
    } else {
      categoryPath = '宠物玩具 > ' + (categoryId === '31' ? '互动玩具' : '益智玩具')
    }
    
    const productName = Mock.Random.ctitle(5, 10)
    
    // 根据关键词过滤
    if (keyword && !productName.includes(keyword)) {
      continue
    }
    
    const product = {
      id: Mock.Random.guid(),
      name: productName,
      categoryId: categoryId,
      categoryPath: categoryPath,
      images: [
        Mock.Random.image('200x200', Mock.Random.color(), '#FFF', 'png', productName.substring(0, 1)),
        Mock.Random.image('200x200', Mock.Random.color(), '#FFF', 'png', productName.substring(0, 1))
      ],
      price: Mock.Random.float(10, 1000, 2, 2),
      minPrice: Mock.Random.float(10, 500, 2, 2),
      maxPrice: Mock.Random.float(500, 1000, 2, 2),
      fixedPrice: Mock.Random.float(100, 800, 2, 2),
      owner: Mock.Random.cname(),
      availableTime: Mock.Random.pick(['周一至周五 9:00-18:00', '全天', '工作日', '周末']),
      status: productStatus,
      isOnShelf: productShelf,
      description: Mock.Random.cparagraph(1, 3),
      createTime: Mock.Random.date('yyyy-MM-dd')
    }
    
    list.push(product)
  }
  
  // 分页处理
  const filteredList = list.slice((page - 1) * limit, page * limit)
  
  return {
    code: 200,
    msg: 'success',
    data: {
      list: filteredList,
      total: list.length
    }
  }
}

// 获取商品详情
const getProductDetail = (id) => {
  return {
    code: 200,
    msg: 'success',
    data: {
      id: id,
      name: Mock.Random.ctitle(5, 10),
      categoryId: Mock.Random.pick(['11', '12', '21', '22', '23', '31', '32']),
      categoryPath: Mock.Random.pick(['宠物用品 > 猫咪用品', '宠物用品 > 狗狗用品', '宠物食品 > 猫粮', '宠物食品 > 狗粮', '宠物食品 > 零食', '宠物玩具 > 互动玩具', '宠物玩具 > 益智玩具']),
      images: [
        Mock.Random.image('200x200', Mock.Random.color(), '#FFF', 'png', 'Product'),
        Mock.Random.image('200x200', Mock.Random.color(), '#FFF', 'png', 'Product')
      ],
      price: Mock.Random.float(10, 1000, 2, 2),
      minPrice: Mock.Random.float(10, 500, 2, 2),
      maxPrice: Mock.Random.float(500, 1000, 2, 2),
      fixedPrice: Mock.Random.float(100, 800, 2, 2),
      owner: Mock.Random.cname(),
      availableTime: Mock.Random.pick(['周一至周五 9:00-18:00', '全天', '工作日', '周末']),
      status: Mock.Random.pick(['1', '2', '3', '4']),
      isOnShelf: Mock.Random.boolean(),
      description: Mock.Random.cparagraph(1, 3),
      createTime: Mock.Random.date('yyyy-MM-dd')
    }
  }
}

// 模拟API接口
Mock.mock(/\/pet\/product\/list/, 'get', (options) => {
  const url = options.url
  const params = {}
  
  // 解析URL参数
  url.replace(/\?(.*)$/, (_, search) => {
    search.split('&').forEach(item => {
      const [key, value] = item.split('=')
      params[key] = decodeURIComponent(value)
    })
  })
  
  return generateProductList(params)
})

Mock.mock(/\/pet\/product\/categories/, 'get', () => {
  return {
    code: 200,
    msg: 'success',
    data: categories
  }
})

Mock.mock(/\/pet\/product\/\w+$/, 'get', (options) => {
  const url = options.url
  const id = url.match(/\/pet\/product\/(\w+)$/)[1]
  return getProductDetail(id)
})

Mock.mock(/\/pet\/product\/add/, 'post', () => {
  return {
    code: 200,
    msg: 'success',
    data: null
  }
})

Mock.mock(/\/pet\/product\/update/, 'put', () => {
  return {
    code: 200,
    msg: 'success',
    data: null
  }
})

Mock.mock(/\/pet\/product\/status/, 'put', () => {
  return {
    code: 200,
    msg: 'success',
    data: null
  }
})

Mock.mock(/\/pet\/product\/\w+$/, 'delete', () => {
  return {
    code: 200,
    msg: 'success',
    data: null
  }
})